# -*- coding: utf-8 -*-
import Util#Util.getDict
from req import  Req
from pipe import Pipe
import random
from mongoBase import MongoDBBase
class ReqFactory(object):
    def __init__(self,factory_name,user_to_fetcher_pipe):
        self._factory_name=factory_name
        self.pipe=user_to_fetcher_pipe
    def work(self,req):
        if(type(req) is not Req):
            raise Exception
        unit_name=req.unit_info["unit_name"]
        dataset_from_commit=req.data_info["dataset_from_commit"]
        req.fetch_info=self.get_fetch_info(unit_name=unit_name,dataset_from_commit=dataset_from_commit)
        self.pipe.put(req)
    def get_fetch_info(self,unit_name,dataset_from_commit):
        # param dataset_from_commit is the same as Req.data_info["dataset_from_commit"]
        if unit_name=="SampleJudge":
            fetch_info={}
            fetch_info["fetch_scheme"]="SampleJudge"
            #todo need to bundle
            uid_list=[]
            pid_list=[]
            tid_list=[]
            ans_list=[]
            for item in dataset_from_commit["uid_pid_tid_ans_list"]:
                if uid_list.count(item["uid"])==0:
                    uid_list.append(item["uid"])
                if pid_list.count(item["pid"])==0:
                    pid_list.append(item["pid"])
                if tid_list.count(item["tid"])==0:
                    tid_list.append(item["tid"])
                if ans_list.count(item["ans"])==0:
                    ans_list.append(item["ans"])

            fetch_info["fetch_param_set"] = Util.getDict(uid_list=uid_list,pid_list=pid_list,tid_list=tid_list,ans_list=ans_list)
            return  fetch_info

if __name__ == '__main__':

    myReq_list=[]
    for i in range(5):
        input_info={}
        input_info["unit_name"]="SampleJudge"
        input_info["dataset_from_commit"]={}
        input_info["dataset_from_commit"]["uid_pid_tid_ans_list"]=[]
        for i in range(random.choice([1,2,3,4,5])):
            input_info["dataset_from_commit"]["uid_pid_tid_ans_list"].append(Util.getDict(
                uid=random.choice([1,2,3,4,5,-1,0]),pid=random.choice([1,2,3,4,5,-1,0]),tid=random.choice([1,2,3,4,5,-1,0]),ans=random.choice([1,2,3,4,5,-1,0])))
        print(input_info)
        i=Req(id=i,input_info=input_info)
        myReq_list.append(i)

    user_to_fetcher_pipe=Pipe()
    myReqFactory=ReqFactory("myReqFactory",user_to_fetcher_pipe)
    for r in myReq_list:
        myReqFactory.work(r)
    for r in myReq_list:
        print (r.fetch_info)

